﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Personal;

namespace Job.Web.userControls.common
{
    public partial class PersonSearcher1Info : BaseCtrl
    {
        private int SearcherId
        {
            get { return myR["id"].IntValue; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (LoadData())
                {
                    SetAccess();
                }
                else
                {
                    this.btnSave.Enabled = false;
                    this.btnSearch.Enabled = false;
                }
            }
        }

        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color='red'>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }

        private void SetAccess()
        {
            MySession session = new MySession();
            bool haveAccess = (session.GetAdminUserFromSession() != null);
            this.adminTr0.Visible = haveAccess;
            this.adminTr1.Visible = haveAccess;

            if (!haveAccess)
            {
                this.ddlState.SelectedValue = "1";
                this.tbLogin.Text = "";
            }
        }

        private bool LoadData()
        {
            JobPerson1Searcher searcher;
            if (SearcherId > 0)
            {
                searcher = new SearcherLogic().GetSearcher(SearcherId);
                if (null == searcher)
                {
                    ShowMessage("无法查询到搜索器定义的内容！");
                    return false;
                }
                else
                {
                    MySession session = new MySession();
                    SessionPersonal person = session.GetPersonalFromSession();
                    if (!searcher.PersonId.Equals(person.PersonAccountid)
                        || !searcher.PersonAccount.Equals(person.LoginName))
                    {
                        ShowMessage("您无权管理指定的搜索器！");
                    }
                }
            }
            else
            {
                searcher = new JobPerson1Searcher();
            }
            this.ShowInput(searcher);
            return true;
        }

        private void ShowInput(JobPerson1Searcher searcher)
        {
            tbKey.Text = (searcher.SearchKey == null ) ? "" : searcher.SearchKey;
            tbName.Text = (searcher.CompanyName == null ) ? "" : searcher.CompanyName;

            ddlRegistDiff.SelectedValue = searcher.RegistDiff.ToString();
            ddlLoginDiff.SelectedValue = searcher.LoginDiff.ToString();

            string property =  searcher.CompanyProperty.ToString();
            WebCommon.SetDropDownFromDict(ddlProperty, property,
                Constants.GetDictionaryCode(Constants.DictionaryType.COMPANY_PROPERTY), true);

            WebCommon.LoadLevelDropDownList(ddlIndustryFather, ddlIndustrySub, IndustryRelate,
                Constants.GetParentCodeBySubCode(searcher.CompanyIndustry), searcher.CompanyIndustry,
                Constants.DictionaryType.INDUSTRY_CLASS);

            string scope = searcher.CompanyScope.ToString();
            WebCommon.SetDropDownFromDict(ddlScope, scope,
                Constants.GetDictionaryCode(Constants.DictionaryType.COMPANY_SCOPE), true);


            WebCommon.LoadLevelDropDownList(ddlAddrFather, ddlAddrSub, AddressRate,
                Constants.GetParentCodeBySubCode(searcher.CompanyAddr), searcher.CompanyAddr,
                Constants.DictionaryType.ADDR_CITY);

            tbCorporation.Text = 
                (null == searcher.CompanyCorporation) ? "" : searcher.CompanyCorporation;
            tbLinkman.Text = (null == searcher.ContactMan) ? "" : searcher.ContactMan;

            ddlState.SelectedValue = searcher.AccountStatus.ToString();
            tbLogin.Text = (null == searcher.LoginName) ? "" : searcher.LoginName;
            tbSearcherName.Text = searcher.SearcherName;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                JobPerson1Searcher searcher = GetSearcher();

                SearcherLogic logic = new SearcherLogic();
                if (SearcherId > 0)
                {
                    searcher.SearcherId = SearcherId;
                    logic.UpdateSearcher(searcher);
                }
                else
                {
                    logic.InsertSearcher(searcher);
                }

                MySession session = new MySession();
                if (null != session.GetAdminUserFromSession())
                {
                    string url = WebCommon.GetApplicationPath(Request)
                        + "AdminWeb/AdminMain.aspx?controlType="
                        + ((int)Constants.SubjectType.PersonSearhcer1).ToString();
                    Response.Redirect(url);
                    return;
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                ShowMessage("保存搜索器失败：" + ex.Message);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                JobPerson1Searcher seacher = GetSearcher();
                String key = "C" + DateTime.Today.Ticks.ToString();
                Cache[key] = seacher;

                MySession session = new MySession();
                string url = "";

                if (null != session.GetAdminUserFromSession())
                {
                    url = WebCommon.GetApplicationPath(Request)
                        + "AdminWeb/AdminMain.aspx?controlType="
                        + ((int)Constants.SubjectType.PersonSearhcer1).ToString()
                        + "&type=1&key=" + key;
                }
                else
                {
                }
                Response.Redirect(url);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex.StackTrace);
                ShowMessage("内部异常，无法查询求职者：" + ex.Message);
            }
        }

        private JobPerson1Searcher GetSearcher()
        {
            JobPerson1Searcher searcher = new JobPerson1Searcher();
            try
            {
                searcher.SearchKey = tbKey.Text.Trim();
                searcher.CompanyName = tbName.Text.Trim();
                searcher.RegistDiff = Convert.ToInt32(ddlRegistDiff.SelectedValue);
                searcher.LoginDiff = Convert.ToInt32(ddlLoginDiff.SelectedValue);
                searcher.CompanyProperty = Convert.ToInt32(ddlProperty.SelectedValue);

                if ( ddlIndustrySub.SelectedValue.Length > 1 )
                    searcher.CompanyIndustry = ddlIndustrySub.SelectedValue;
                else if ( ddlIndustryFather.SelectedValue.Length > 1 )
                    searcher.CompanyIndustry = ddlIndustryFather.SelectedValue;
                else
                    searcher.CompanyIndustry = "";

                searcher.CompanyScope = Convert.ToInt32(ddlScope.SelectedValue);

                if ( ddlAddrSub.SelectedValue.Length > 1 )
                    searcher.CompanyAddr = ddlAddrSub.SelectedValue;
                else if (ddlAddrFather.SelectedValue.Length > 1 )
                    searcher.CompanyAddr = ddlAddrFather.SelectedValue;
                else
                    searcher.CompanyAddr = "";

                searcher.CompanyCorporation = tbCorporation.Text.Trim();
                searcher.ContactMan = tbLinkman.Text.Trim();
                searcher.AccountStatus = Convert.ToInt32(ddlState.SelectedValue);
                searcher.LoginName = tbLogin.Text.Trim();
                searcher.SearcherName = tbSearcherName.Text.Trim();
                
                MySession session = new MySession();
                searcher.PersonId = session.GetPersonalFromSession().PersonAccountid;
                searcher.PersonAccount = session.GetPersonalFromSession().LoginName;
            }
            catch (Exception e)
            {
                Logger.WriteLog(e.Message);
            }
            return searcher;
        }


    }
}